package com.smec.moblie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.smec.moblie.R;
import com.smec.moblie.bean.AttachmentBean;

import java.util.ArrayList;
import java.util.List;

import static com.smec.moblie.control.MainControl.getFile;

/**
 * Created by sunyubo on 2017/12/7.
 */

public class AttachmentListAdapter extends BaseAdapter {
    private List<AttachmentBean> attachmentBeanList;
    private LayoutInflater inflater;
    private Context context;

    public AttachmentListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        attachmentBeanList = new ArrayList<>();
        this.context = context;
    }

    public void setList(List<AttachmentBean> attachmentBeanList) {
        this.attachmentBeanList = attachmentBeanList;
    }

    @Override
    public int getCount() {
        return attachmentBeanList.size();
    }

    @Override
    public AttachmentBean getItem(int position) {
        return attachmentBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = inflater.inflate(R.layout.adapter_attachment_list, null);
        } else {
            view = convertView;
        }
        TextView textViewName = (TextView) view.findViewById(R.id.textViewName);

        AttachmentBean attachmentBean = attachmentBeanList.get(position);
        textViewName.setText(attachmentBean.getName());

        ImageView imageViewDownLoad = (ImageView) view.findViewById(R.id.imageViewDownLoad);
        imageViewDownLoad.setOnClickListener(new MyOnClickListener(attachmentBean));
        view.setOnClickListener(new MyOnClickListener(attachmentBean));
        return view;
    }

    private class MyOnClickListener implements View.OnClickListener {
        AttachmentBean attachmentBean;

        public MyOnClickListener(AttachmentBean attachmentBean) {
            this.attachmentBean = attachmentBean;
        }

        @Override
        public void onClick(View v) {
            new AlertView("提示", "是否要下载" + attachmentBean.getName(), "取消", new String[]{"确定"}, null, context, AlertView.Style.Alert, new OnItemClickListener() {
                @Override
                public void onItemClick(Object o, int position) {
                    if (position == 0) {
                        getFile(attachmentBean.getUrl(), attachmentBean.getName(), context);
                    }
                }
            }).show();
        }
    }
}
